//
//  UIViewController+Addition.m
//  BaseProject
//
//  Created by zhangrongbing on 2016/11/7.
//  Copyright © 2016年 lovcreate. All rights reserved.
//

#import "UIViewController+Addition.h"

@implementation UIViewController (Addition)

+(void)load{
    [self exchangeInstanceMethod1:@selector(presentViewController:animated:completion:) method2:@selector(lc_presentViewController:animated:completion:)];
}

-(void)lc_presentViewController:(UIViewController*)controller animated:(BOOL)animated completion:(void (^ __nullable)(void))completion{
    if ([controller isKindOfClass:[UINavigationController class]]) {
        UINavigationController* nav = (UINavigationController*)controller;
        UIViewController* rootVC = [nav.viewControllers lastObject];
        UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(pressCancelItem:)];
        rootVC.navigationItem.leftBarButtonItem = item;
    }
    
    [self lc_presentViewController:controller animated:animated completion:completion];
}

-(void)pressCancelItem:(UIBarButtonItem*)item{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)lc_showToastMessage:(NSString*)message{
    UIApplication* application = [UIApplication sharedApplication];
    MBProgressHUD* toastHud = [MBProgressHUD HUDForView:application.keyWindow];
    if (!toastHud) {
        toastHud = [[MBProgressHUD alloc] initWithView:application.keyWindow];
        toastHud.mode = MBProgressHUDModeText;
        toastHud.removeFromSuperViewOnHide = YES;
    }
    toastHud.label.text = message;
    toastHud.offset = CGPointMake(0, MBProgressMaxOffset);
    [application.keyWindow addSubview:toastHud];
    [toastHud showAnimated:YES];
    [toastHud hideAnimated:YES afterDelay:1];
}

//-(MBProgressHUD*)lc_showHUD{
//    UIApplication* application = [UIApplication sharedApplication];
//    MBProgressHUD* hud = [[MBProgressHUD alloc] initWithFrame:application.keyWindow.bounds];
//    hud.removeFromSuperViewOnHide = YES;
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.backgroundView.color = [[UIColor grayColor] colorWithAlphaComponent:.4];
//    hud.mode = MBProgressHUDModeIndeterminate;
//    [application.keyWindow addSubview:hud];
//    [hud showAnimated:YES];
//    return hud;
//}
@end

@implementation UINavigationController(Addition)

//+(void)load{
//    
//    [self exchangeInstanceMethod1:@selector(pushViewController:animated:) method2:@selector(lc_pushViewController:animated:)];
//}
//
//-(void)lc_pushViewController:(UIViewController*)viewController animated:(BOOL)animated{
//    if (self.childViewControllers.count > 0 ) {
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewControllerAnimated:)];
//    }
//    [self lc_pushViewController:viewController animated:YES];
//}
@end
